namespace DotNetNuke.UI.WebControls
{
    using Microsoft.VisualBasic;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;
    using System.IO;
    using System.Reflection;
    using System.Text;
    using System.Xml;
    using System.Xml.XPath;
    using System.Xml.Xsl;

    public class DNNNodeCollection : CollectionBase
    {
        private XmlDocument m_objXMLDoc;
        private XmlNode m_objXMLNode;

        public DNNNodeCollection(string strNamespace)
        {
            this.m_objXMLDoc = new XmlDocument();
            this.m_objXMLNode = this.m_objXMLDoc.CreateNode(XmlNodeType.Element, "root", "");
            XmlAttribute node = this.m_objXMLDoc.CreateAttribute("id");
            node.Value = strNamespace;
            this.m_objXMLNode.Attributes.Append(node);
            this.m_objXMLDoc.AppendChild(this.m_objXMLNode);
        }

        public DNNNodeCollection(XmlNode objXmlNode)
        {
            this.m_objXMLNode = objXmlNode;
            this.m_objXMLDoc = this.m_objXMLNode.OwnerDocument;
        }

        public DNNNodeCollection(string strXML, string strXSLFile)
        {
            this.m_objXMLDoc = new XmlDocument();
            if (Strings.Len(strXSLFile) > 0)
            {
                this.m_objXMLDoc.LoadXml(this.DoTransform(strXML, strXSLFile));
            }
            else
            {
                this.m_objXMLDoc.LoadXml(strXML);
            }
            this.m_objXMLNode = this.m_objXMLDoc.SelectSingleNode("//root");
        }

        public int Add()
        {
            DNNNode node = new DNNNode();
            this.XMLNode.AppendChild(this.XMLDoc.ImportNode(node.XmlNode, false));
            node.ID = node.ParentNameSpace + "_" + StringType.FromInteger(this.XMLNode.ChildNodes.Count);
            return (this.XMLNode.ChildNodes.Count - 1);
        }

        public int Add(DNNNode objNode)
        {
            XmlNode newChild = this.XMLDoc.ImportNode(objNode.XmlNode, true);
            this.XMLNode.AppendChild(newChild);
            objNode.AssociateXmlNode(newChild);
            return (this.XMLNode.ChildNodes.Count - 1);
        }

        public int Add(string strID, string strKey, string strText, string strNavigateURL, string strJSFunction, string strTarget, string strToolTip, bool blnEnabled, string strCSSClass, string strCSSClassSelected, string strCSSClassHover)
        {
            int num2 = this.Add();
            DNNNode node = this[num2];
            if (Strings.Len(strID) > 0)
            {
                node.ID = strID;
            }
            node.Key = strKey;
            node.Text = strText;
            node.NavigateURL = strNavigateURL;
            node.JSFunction = strJSFunction;
            node.Target = strTarget;
            node.ToolTip = strToolTip;
            node.Enabled = blnEnabled;
            node.CSSClass = strCSSClass;
            node.CSSClassSelected = strCSSClassSelected;
            node.CSSClassHover = strCSSClassHover;
            return num2;
        }

        public int AddBreak()
        {
            int num2 = this.Add();
            DNNNode node = this[num2];
            node.IsBreak = true;
            return num2;
        }

        public void Clear()
        {
            for (int i = this.XMLNode.ChildNodes.Count - 1; i >= 0; i += -1)
            {
                this.XMLNode.RemoveChild(this.XMLNode.ChildNodes[i]);
            }
        }

        public bool Contains(DNNNode value)
        {
            if (this.FindNode(value.ID) == null)
            {
                return false;
            }
            return true;
        }

        public void CopyTo(Array myArr, int index)
        {
            IEnumerator enumerator=null;
            try
            {
                enumerator = this.m_objXMLNode.ChildNodes.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    XmlNode current = (XmlNode) enumerator.Current;
                    myArr.SetValue(current, index);
                    index++;
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    ((IDisposable) enumerator).Dispose();
                }
            }
        }

        private string DoTransform(string XML, string XSL)
        {
            return this.DoTransform(XML, XSL, null);
        }

        private string DoTransform(string XML, string XSL, XsltArgumentList Params)
        {
            string str;
            try
            {
                XslTransform transform = new XslTransform();
                transform.Load(XSL);
                XmlTextReader reader = new XmlTextReader(XML, XmlNodeType.Document, null);
                XPathDocument document = new XPathDocument(reader);
                XmlUrlResolver resolver = new XmlUrlResolver();
                StringBuilder sb = new StringBuilder();
                StringWriter writer = new StringWriter(sb, null);
                transform.Transform((IXPathNavigable) document, Params, (TextWriter) writer, resolver);
                writer.Close();
                str = sb.ToString();
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                throw exception;
                
            }
            return str;
        }

        public DNNNode FindNode(string ID)
        {
            XmlNode objXmlNode = this.XMLNode.SelectSingleNode(".//n[@id='" + ID + "']");
            if (objXmlNode != null)
            {
                return new DNNNode(objXmlNode);
            }
            return null;
        }

        public DNNNode FindNodeByKey(string Key)
        {
            XmlNode objXmlNode = this.XMLNode.SelectSingleNode(".//n[@key='" + Key + "']");
            if (objXmlNode != null)
            {
                return new DNNNode(objXmlNode);
            }
            return null;
        }

        public virtual Collection FindSelectedNodes()
        {
            Collection collection = new Collection();
            if (this.XMLNode != null)
            {
                XmlNodeList list = this.XMLNode.SelectNodes("//n[@selected='1']");
                foreach (XmlNode node in list)
                {
                    collection.Add(new DNNNode(node), null, null, null);
                }
            }
            return collection;
        }

        public IEnumerator GetEnumerator()
        {
            return new DNNNodeEnumerator(this.m_objXMLNode);
        }

        public int Import(DNNNode objNode)
        {
            return this.Import(objNode, true);
        }

        public int Import(DNNNode objNode, bool blnDeep)
        {
            XmlNode newChild = this.XMLDoc.ImportNode(objNode.XmlNode, blnDeep);
            this.XMLNode.AppendChild(newChild);
            return (this.XMLNode.ChildNodes.Count - 1);
        }

        public int IndexOf(DNNNode value)
        {
            int num2=0;
            int num3 = this.XMLNode.ChildNodes.Count - 1;
            for (int i = 0; i <= num3; i++)
            {
                if (StringType.StrCmp(new DNNNode(this.XMLNode.ChildNodes[i]).ID, value.ID, false) == 0)
                {
                    return i;
                }
            }
            return num2;
        }

        public void InsertAfter(int index, DNNNode value)
        {
            XmlNode newChild = this.XMLDoc.ImportNode(value.XmlNode, true);
            this.XMLNode.InsertAfter(newChild, this.XMLNode.ChildNodes[index]);
        }

        public void InsertBefore(int index, DNNNode value)
        {
            XmlNode newChild = this.XMLDoc.ImportNode(value.XmlNode, true);
            this.XMLNode.InsertBefore(newChild, this.XMLNode.ChildNodes[index]);
        }

        protected override void OnClear()
        {
            this.Clear();
        }

        protected override void OnInsert(int index, object value)
        {
            this.InsertAfter(index, (DNNNode) value);
        }

        protected override void OnRemove(int index, object value)
        {
            this.Remove((DNNNode) value);
        }

        protected override void OnSet(int index, object oldValue, object newValue)
        {
            this[index] = (DNNNode) newValue;
        }

        public void Remove(DNNNode value)
        {
            this.XMLNode.RemoveChild(value.XmlNode);
        }

        public string ToXml()
        {
            return this.XMLDoc.OuterXml;
        }

        public int Count
        {
            get
            {
                return this.m_objXMLNode.ChildNodes.Count;
            }
        }

        public bool IsSynchronized
        {
            get
            {
                return false;
            }
        }

        public DNNNode this[int index]
        {
            get
            {
                return new DNNNode(this.m_objXMLNode.ChildNodes[index]);
            }
            set
            {
                throw new Exception("Cannot Assign Node Directly");
            }
        }

        public object SyncRoot
        {
            get
            {
                return this;
            }
        }

        public XmlDocument XMLDoc
        {
            get
            {
                return this.m_objXMLDoc;
            }
        }

        public XmlNode XMLNode
        {
            get
            {
                return this.m_objXMLNode;
            }
        }
    }
}

